/*
 * @Author: your name
 * @Date: 2021-10-09 10:27:31
 * @LastEditTime: 2021-10-09 10:53:33
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \test_algorithm\threadsafe_map.hpp
 */

#include <map>
#include <mutex>

template<typename Key, typename Val>
class ThreadSafeMap
{
public:
	typedef typename std::map<Key, Val>::iterator this_iterator;
	typedef typename std::map<Key, Val>::const_iterator this_const_iterator;
	Val& operator [](const Key& key) 
	{
		std::lock_guard<std::mutex> lk(mtx_);
		return dataMap_[key];
	}
	int erase(const Key& key )
	{
		std::lock_guard<std::mutex> lk(mtx_);
		return dataMap_.erase(key);
	}
 
	this_iterator find( const Key& key )
	{
		std::lock_guard<std::mutex> lk(mtx_);
		return dataMap_.find(key);
	}
	this_const_iterator find( const Key& key ) const
	{
		std::lock_guard<std::mutex> lk(mtx_);
		return dataMap_.find(key);
	}
	
	this_iterator end()
	{
		return dataMap_.end();
	}
 
	this_const_iterator end() const
	{
		return dataMap_.end();
	}
	
private:
	std::map<Key, Val> dataMap_;	
	std::mutex mtx_;
};
